package com.prs.crm.action.project;

import java.io.File;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.Currency;
import com.prs.crm.domain.project.Project;
import com.prs.crm.domain.project.ProjectSource;
import com.prs.crm.domain.project.ProjectStage;
import com.prs.crm.domain.project.ProjectType;
import com.prs.crm.domain.project.Task;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.CurrencyService;
import com.prs.crm.service.project.ProjectService;
import com.prs.crm.service.project.ProjectSourceService;
import com.prs.crm.service.project.ProjectStageService;
import com.prs.crm.service.project.ProjectTypeService;

public class ProjectAction extends FlowAction<Project> {

	private static final long serialVersionUID = 1L;

	private ProjectService projectService;

	private ProjectSourceService projectSourceService;

	private ProjectTypeService projectTypeService;

	private ProjectStageService projectStageService;

	private CurrencyService currencyService;

	private Collection<ProjectSource> sourcelist;

	private Collection<ProjectType> typelist;

	private Collection<ProjectStage> stagelist;

	private Collection<Currency> currencylist;

	private Project project;

	private PaginationSupport pagination;

	private String joiner;

	private Task task;

	private File[] upload;

	private String[] uploadFileName;

	private Attachment reference;
	private String flow;

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public File[] getUpload() {
		return upload;
	}

	public Project getProject() {
		return project;
	}

	public Task getTask() {
		return task;
	}

	public void setJoiner(String joiner) {
		this.joiner = joiner;
	}

	public String getJoiner() {
		return joiner;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Attachment getReference() {
		return reference;
	}

	public void setReference(Attachment reference) {
		this.reference = reference;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public void setProjectSourceService(
			ProjectSourceService projectSourceService) {
		this.projectSourceService = projectSourceService;
	}

	public ProjectSourceService getProjectSourceService() {
		return projectSourceService;
	}

	public void setProjectTypeService(ProjectTypeService projectTypeService) {
		this.projectTypeService = projectTypeService;
	}

	public ProjectTypeService getProjectTypeService() {
		return projectTypeService;
	}

	public void setProjectStageService(ProjectStageService projectStageService) {
		this.projectStageService = projectStageService;
	}

	public ProjectStageService getProjectStageService() {
		return projectStageService;
	}

	public void setCurrencyService(CurrencyService currencyService) {
		this.currencyService = currencyService;
	}

	public CurrencyService getCurrencyService() {
		return currencyService;
	}

	public void setSourcelist(Collection<ProjectSource> sourcelist) {
		this.sourcelist = sourcelist;
	}

	public Collection<ProjectSource> getSourcelist() {
		return sourcelist;
	}

	public void setTypelist(Collection<ProjectType> typelist) {
		this.typelist = typelist;
	}

	public Collection<ProjectType> getTypelist() {
		return typelist;
	}

	public void setStagelist(Collection<ProjectStage> stagelist) {
		this.stagelist = stagelist;
	}

	public Collection<ProjectStage> getStagelist() {
		return stagelist;
	}

	public void setCurrencylist(Collection<Currency> currencylist) {
		this.currencylist = currencylist;
	}

	public Collection<Currency> getCurrencylist() {
		return currencylist;
	}

	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

	private void prepareEdit() {
		this.setSourcelist(this.getProjectSourceService().getAllSource());
		this.setTypelist(this.getProjectTypeService().getAllType());
		this.setStagelist(this.getProjectStageService().getAllStage());
		this.setCurrencylist(this.getCurrencyService().getAllCurrency());
	}

	@Action("listCreateProject")
	public String listProject() {
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProjectService().listCreateProject(
				pagination, AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("listVaryProject")
	public String listVaryProject() {
		return this.listProject();
	}

	@Action("varyProject")
	public String varyProject() {
		this.prepareEdit();
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "executeVaryProject", results = { @Result(name = "success", type = "redirect", location = "listVaryProject") })
	public String executeVaryProject() {
		this.getProjectService().executeVaryProject(project, upload,
				uploadFileName);
		this.setPrompt("变更成功");
		return SUCCESS;
	}

	@Action(value = "removeProjectReference", results = { @Result(name = "success", type = "redirect", location = "varyProject?project=${project.id}") })
	public String removeProjecReference() {
		this.getProjectService().removeProjectReference(project, reference);
		return SUCCESS;
	}

	@FlowActive(target = "project", flowservice = "projectService")
	@Action("createProject")
	public String createProject() {
		prepareEdit();
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@FlowStart(flowservice = "projectService", target = "project")
	@Action(value = "saveProject", results = {
			@Result(name = "success", type = "redirect", location = "createProject"),
			@Result(name = "error", type = "chain", location = "createProject") })
	public String saveProject() {
		if (flow == null || flow.equals("")) {
			this.addActionError("审核流程不能为空");
			return ERROR;
		}
		if (project.getName() == null || "".equals(project.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (project.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (task.getExecutor() == null) {
			addActionError("执行人不能为空");
			return ERROR;
		}
		if (task.getGrade() == null) {
			addActionError("级别不能为空");
			return ERROR;
		}
		if (task.getCycles() == null) {
			addActionError("周期不能为空");
			return ERROR;
		}
		if (joiner == null || "".equals(joiner.trim())) {
			addActionError("参与人不能为空");
			return ERROR;
		}
		if (project.getDistrict() == null) {
			addActionError("地区不能为空");
			return ERROR;
		}
		if (project.getStartTime() == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (project.getEndTime() == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (project.getSource() == null) {
			addActionError("来源不能为空");
			return ERROR;
		}
		if (project.getStage() == null) {
			addActionError("阶段不能为空");
			return ERROR;
		}
		try {
			this.getProjectService().createProject(project, task, joiner,
					upload, uploadFileName);
			this.setPrompt("项目创建成功");
			return SUCCESS;
		} catch (InValidException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}

	@Action("viewProject")
	public String viewProject() {
		return SUCCESS;
	}

	@FlowActive(target = "project", flowservice = "projectService")
	@Action("projectInfo")
	public String projectInfo() {
		return SUCCESS;
	}

	@FlowStart(target = "project", flowservice = "projectService")
	@Action(value = "submitProject", results = {
			@Result(name = "success", type = "chain", location = "projectInfo"),
			@Result(name = "error", type = "chain", location = "projectInfo") })
	public String submitProject() {
		this.getProjectService().save(project);
		this.setPrompt("提交成功");
		return SUCCESS;
	}

	@Action("listHistoryProject")
	public String listHistoryProject() {
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProjectService().listHistoryProject(
				pagination, AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("historyProjectInfo")
	public String historyProjectInfo() {
		return SUCCESS;
	}

	@Action("listAllProject")
	public String listAllProject() {
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProjectService().listAllProject(pagination));
		return SUCCESS;
	}

}
